Skip to content

feat(otel)!: upgrade to OpenTelemetry JS SDK 3 and fix span parenting - #126

Merged
Julien-R44 merged 2 commits into
mainfrom
feat/otel-sdk-3
Nov 11, 2026
Merged

Julien-R44 merged 2 commits into
mainfrom
feat/otel-sdk-3

Conversation

@Julien-R44

Copy link
Copy Markdown
Owner

Summary

Two commits:

  1. feat(otel)!: upgrade @bentocache/otel to OpenTelemetry JS SDK 3. @opentelemetry/core moves to ^3.1.1 and @opentelemetry/instrumentation to ^0.301.1. @opentelemetry/api becomes a ^1.10.0 peer dependency, as in the upstream SDK 3 instrumentations. The package now requires Node.js >=22.15.0. Tests use TracerProvider from @opentelemetry/sdk-trace instead of sdk-trace-base. The instrumentation source needed no changes for SDK 3.

  2. fix(otel): make cache operation spans the active context. Parent spans used to be resolved through a map keyed by store:key, which caused these problems:

    • Operations from concurrent requests on the same key were attached to another request's getOrSet span, so they ended up in another trace.
    • Spans created inside a factory (database queries, nested getOrSet) were not children of cache.factory.

    The span is now attached with context.attach() (@opentelemetry/api 1.10) on the tracing channel start event, and the token is disposed on the synchronous end event. This commit also:

    • reports the package version as the instrumentation version (it was hardcoded to 0.1.0);
    • logs a warning when manuallyRegister() cannot patch the read-only ESM namespace;
    • fixes the test helper, which never actually patched the constructor, so suppressInternalOperations was untested.

Breaking change

The two SDK lines cannot share one release. SDK 2 packages require @opentelemetry/api <1.10.0, and SDK 3 packages require >=1.10.0 <1.11.0. With npm, an SDK 2 app now fails with ERESOLVE on the @opentelemetry/api peer. The changesets release this as 0.2.0, so ^0.1.x consumers stay on SDK 2.

Verification

  • pnpm build, typecheck, oxlint --deny-warnings and oxfmt --check pass. Tests: 11/11.
  • New regression tests cover the cross-trace contamination, factory children and L2 suppression cases. The two parenting tests fail on the previous instrumentation.
  • Scenario runs printed the span trees for these cases, with identical results on Node 26 and Node 22.15.0:
    • concurrent requests on the same key
    • nested getOrSet
    • factory error, grace fallback and SWR
    • L2 suppression
    • context leak checks
  • A packed tarball installed with npm in a separate SDK 3 app, using the real import-in-the-middle hook, produced correctly parented spans and suppressed L2 spans.

Not included

  • playground/ still lists SDK 2 dependencies. They are unused, and its dev script imports a missing src/instrumentation.ts.
  • suppressInternalOperations cannot work with manuallyRegister() on ESM. Making it work would need a new exported internalOperationWrapper helper.

Move @opentelemetry/core to 3.x and @opentelemetry/instrumentation to 0.301.x,
make @opentelemetry/api a ^1.10.0 peer dependency and require Node.js >=22.15.0.
Tests now run against the SDK 3 TracerProvider from @opentelemetry/sdk-trace.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1cc89-88ff-735a-b0e6-546b99965804
Parent spans were resolved through a map keyed by store and key, so operations
from concurrent requests on the same key were attached to another request's
getOrSet span, and spans created inside a factory were not children of the
factory span. Attach the operation span with context.attach() on the tracing
channel start event and dispose it on the synchronous end event instead.

Also report the package version as the instrumentation version, warn when the
BentoCache constructor cannot be patched, and make the test helper patch a
mutable copy of the module so suppressInternalOperations is covered.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1cc89-88ff-735a-b0e6-546b99965804
@changeset-bot

changeset-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0dc91c5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@bentocache/otel Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying bentocache with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0dc91c5
Status:🚫  Build failed.

View logs

@Julien-R44
Julien-R44 merged commit ffbbfbe into main Nov 11, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant